<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="120px"
            class="eidt-el-form"
          >
            <el-form-item label="姓名" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="公司" prop="company">
              <el-input v-model="ruleForm.company"></el-input>
            </el-form-item>
            <el-form-item label="职务" prop="position">
              <el-input v-model="ruleForm.position"></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="type">
              <el-select v-model="ruleForm.type" placeholder="请选择" @change="getType">
                <el-option label="组委会" value="1" :disabled="disabled"></el-option>
                <el-option label="展商" value="2"></el-option>
                <el-option label="工作人员" value="3" :disabled="disabled"></el-option>
                <el-option label="观众" value="4" :disabled="disabled"></el-option>
                <el-option label="媒体" value="5" :disabled="disabled"></el-option>
              </el-select>
            </el-form-item>
            <UploadImg @uploadImg="getUploadImg" :editImg="editImg" :imgTitle="imgTitle"></UploadImg>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>
import UploadImg from "../../public/UploadImg";
import bus from "../../common/bus";

export default {
  name: "editexhibiorspersonal",
  data() {
    return {
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        company: [{ required: true, message: "公司不能为空", trigger: "blur" }],
        position: [
          { required: true, message: "职务不能为空", trigger: "blur" }
        ],
        type: [{ required: true, message: "请选择类型", trigger: "blur" }]
      },
      ruleForm: {
        name: "",
        phone: "",
        company: "",
        position: "",
        avatar: "",
        state: "",
        type: "",
        projectId: ""
      },
      editImg: "",
      personal_Id: "",
      imgTitle: "上传头像",
      disabled:false
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object
    }
  },
  created() {
    var project_Id = localStorage.getItem("project_Id");
    this.projectId = project_Id;
  },
  watch: {
    $route: "getPath",
    editData: function() {
      const item = this.editData;
      this.ruleForm = {
        name: item.name,
        phone: item.phone,
        company: item.company,
        position: item.position,
        type: item.type
      };
      this.personal_Id = item.personalId;
      this.editImg = item.avatar;
    }
  },
  methods: {
    getPath() {
      var router = this.$route.path;
      switch (router) {
        case "/exhibitors_peronal":
          this.ruleForm.type = 2;
          this.disabled = true
          break;
        case "/personal_manage":
          this.ruleForm.type = 1;
          this.disabled = true
          break;
        case "/audience_manage":
          this.ruleForm.type = 4;
          this.disabled = true
          break;
        case "/media_manage":
          this.ruleForm.type = 5;
          this.disabled = true
          break;
        default:
          break;
      }
    },
    saveClick(ruleForm) {
      this.ruleForm = {
        name: this.ruleForm.name,
        phone: this.ruleForm.phone,
        company: this.ruleForm.company,
        position: this.ruleForm.position,
        avatar: this.ruleForm.avatar,
        type: this.ruleForm.type,
        projectId: this.projectId
      };
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.personal_Id == "" || this.personal_Id == undefined) {
            this.$axios.post("/Personal/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
                this.$refs[ruleForm].resetFields();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            this.$axios
              .put("/Personal/" + this.personal_Id + "", this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                  this.$refs[ruleForm].resetFields();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    goBack() {
      this.$emit("backFunStu");
      this.personal_Id = "";
    },
    getUploadImg(imgSrc) {
      this.ruleForm.avatar = imgSrc;
    },
    getType(value) {
      this.ruleForm.type = value;
    }
  },
  components: {
    UploadImg
  }
};
</script>